package com.mobileseed.weather;

import java.util.ArrayList;

import com.mobile.weather.R;
import com.mobileseed.weather.util.config;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class WeatherOptionActivity extends Activity {
	private Spinner sp;
	private String[] strCityArray;
	private String[] strCityPYArray;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setTitle(getString(R.string.menu_option));
		setContentView(R.layout.weatheroption);
		

		//初始化城市设置
		initCitySelectControls();
	}
	
	private boolean initCitySelectControls() {
		
		ArrayList<String> dataSources = new ArrayList<String>();
		strCityArray = getResources().getStringArray(R.array.city_list);
		strCityPYArray = getResources().getStringArray(R.array.citypy_list);

		for (int i = 0; i < strCityArray.length; i++) {
			dataSources.add(strCityArray[i]);
		}

		// 获取控件
		sp = (Spinner) findViewById(R.id.spinnerLocalCity);

		// 创建ArrayAdapter
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, dataSources);

		// 设置选择框样式
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		// 设置适配器
		sp.setAdapter(spinnerAdapter);

		// 设置标题
		sp.setPrompt(getString(R.string.opt_local_city));

		// 设置选择事件
		sp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				try {
					//保存选择的信息
					SharedPreferences settings = getSharedPreferences(config.PREF_NAME, MODE_WORLD_WRITEABLE);
					settings.edit().putString(config.PREF_SEL_CITYPY, strCityPYArray[(int)sp.getSelectedItemId()]).commit();
					settings.edit().putString(config.PREF_SEL_CITYNAME, strCityArray[(int)sp.getSelectedItemId()]).commit();
					
				} catch (Exception ex) {
					System.out.println("select Error: " + ex.getMessage());
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		return true;
	}
}
